import {ProColumns, ProTable} from '@ant-design/pro-components';
import {Modal} from 'antd';
import React, {PropsWithChildren} from 'react';
import {useModel} from "@@/exports";
import {createComment} from "@/services/backend/generatorController";

interface CreateModalProps {
  modalVisible: boolean;
  userId:number;
  goodsId:number;
  onSubmit: () => void;
  onCancel: () => void;
}

/**
 * 创建数据模态框
 * @param props
 * @constructor
 */
const CreateModal: React.FC<PropsWithChildren<CreateModalProps>> = (props) => {
  const { modalVisible, onSubmit, onCancel ,userId,goodsId} = props;
  const { initialState } = useModel('@@initialState');
  const { currentUser } = initialState ?? {};
  /**
   * 表格列配置
   */
  const columns: ProColumns<Admin.CommentResult>[] = [
    {
      dataIndex: 'id',
      valueType: 'indexBorder',
      width: 48,
    },
    {
      title: '评论描述',
      dataIndex: 'commentDescription',
      search: false,
    },
  ];
  return (
    <Modal
      destroyOnClose
      title="评论"
      open={modalVisible}
      onCancel={() => onCancel()}
      footer={null}
    >
      <ProTable<Admin.CommentResult, Admin.CommentResult>
        onSubmit={async (value) => {
          // @ts-ignore
          const success = await createComment(userId,goodsId,value.commentDescription);
          if (success) {
            onSubmit?.();
          }
        }}
        rowKey="id"
        type="form"
        columns={columns}
      />
    </Modal>
  );
};

export default CreateModal;
